/*
 * @Author: humengchuan 531537052@qq.com
 * @Date: 2024-10-08 16:58:11
 * @LastEditors: humengchuan 531537052@qq.com
 * @LastEditTime: 2025-03-17 00:01:51
 * @FilePath: \project\work-tool\experi-api\config\db.config.js
 * @Description: 
 */
const mongoose = require('mongoose');

mongoose.connect("mongodb://127.0.0.1:27017/experi-api");

mongoose.connection.on("open", () => {
    // 在这里可以执行链接成功的操作
    console.log('mongodb 数据库连接成功!')
    
    // 查询并打印 MongoDB 数据库版本
    try {
        mongoose.connection.db.command({ buildInfo: 1 }).then(info => {
            console.log('MongoDB Version:', info.version);
        }).catch(err => {
            console.error("Error fetching MongoDB version:", err);
        });
    } catch (err) {
        console.error("Exception occurred while fetching MongoDB version:", err);
    }
})


// 监听断开连接事件
mongoose.connection.on("disconnected", () => {
    console.log("MongoDB 连接断开")
})

// 监听连接错误事件
mongoose.connection.on("error",(err) => {
    console.error("MongoDB 连接错误:", err)
})